package com.woniuxy.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import com.woniuxy.redis.util.RedisUtil;
import com.woniuxy.user.dto.UserScoreDto;
import com.woniuxy.user.entity.UserScore;
import com.woniuxy.user.service.UserScoreService;
import com.woniuxy.user.util.JWTUtil;
import com.woniuxy.user.form.ChangeUserScoreForm;
import com.woniuxy.user.util.TokenUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@RequestMapping("/userScore")
@Api(tags = "用户积分")
public class UserScoreController {

    @Resource
    private UserScoreService userScoreService;
    @Resource
   private TokenUtil tokenUtil;


    /**
     * 查询用户积分变得详情历史记录
     * 用户自己查自己的
     * @param token
     * @return
     */
    @ApiOperation("查询用户积分变得详情历史记录")
    @GetMapping("/findAllUserScoreById")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = UserScore.class)
    })
    public Result findUserScoreById(@RequestHeader String token){
        Integer userId = tokenUtil.getUserId(token);
        List<UserScore> userScore = userScoreService.list(new QueryWrapper<UserScore>().eq("user_id", userId).orderByDesc("change_time"));
        return Result.success("查询用户历史积分详情成功",userScore);
    }

    /**
     * 查询所有用户积分详情
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("查询所有用户积分详情")
    @GetMapping("/pageAllUserScore")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页"),
            @ApiImplicitParam(name = "pageSize",value = "每页几条")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = UserScoreDto.class)
    })
    @Permission(roles = {"超级管理员","管理员"})

    public Result pageAllUserScore(Integer pageNum,Integer pageSize){
    Page<UserScoreDto> dto= userScoreService.pageAllUserScore(pageNum,pageSize);
        return Result.success("查询所有用户积分详情成功",dto);
    }


    /**
     * 查询最新用户积分
     * @param userId 管理员查询
     * @return
     */
    @ApiOperation("查询最新用户积分")
    @GetMapping("/findUserScoreById")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = UserScore.class)
    })
    public Result findUserScoreById(Integer userId){
        List<UserScore> userScore = userScoreService.list(new QueryWrapper<UserScore>().eq("user_id", userId).orderByDesc("change_time"));
        return Result.success("查询用户最新积分详情成功",userScore.get(0));
    }


    /**
     * 用户积分变动
     * @param form 用户积分变得参数
     * @return
     */
    @ApiOperation("积分变动")
    @PostMapping("/changeUserScore")
    public Result changeUserScore(@Validated @RequestBody ChangeUserScoreForm form, BindingResult br){
       if(br.hasErrors()) throw  new ParamException(10001,"参数有误");
        userScoreService.changeUserScore(form);
        return Result.success("积分变得完成",null);
    }

}

